<template>
  <center>
    <div>
      <van-field v-model="name" label="名字" placeholder="名字" />
      <van-field v-model="mobile" label="手机号" placeholder="手机号" />
      <van-field
        v-model="password"
        type="password"
        label="密码"
        placeholder="请输入密码"
      />
      <van-button type="primary" @click="login">登录</van-button>

      <br /><br /><br /><br />

      <a href="/register">加入顾客</a> &emsp;&emsp;&emsp;
      <a href="/shops_register">入驻商家</a>
    </div>
  </center>
</template>

<script>
export default {
  data() {
    return {
      name: "",
      mobile: "",
      password: "",
    };
  },
  methods: {
    login: function () {
      this.myaxios(this.host + "users/login/", "post", {
        name: this.name,
        mobile: this.mobile,
        password: this.password,
      }).then((data) => {
        console.log(data);
        if (data.errcode == 0) {
          this.$toast.success(data.msg);
          localStorage.setItem("token", data.token);
          localStorage.setItem("id", data.data.id);

          if (data.user_type == null) {
            this.$router.push("/index");
          } else if(data.user_type == 2) {
            this.$router.push("/shops_index");
            localStorage.setItem("user_type", data.user_type);
          }else if(data.user_type == 1){
            this.$router.push("/send_index");
            localStorage.setItem('user_type', data.user_type);
          }
        } else {
          this.$toast.fail(data.msg);
        }
      });
    },
  },
};
</script>

<style>
</style>